package com.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.beans.Department;
import com.beans.Staff;
import com.services.DepartmentService;
import com.services.StaffService;

/**
 * Servlet implementation class StaffServlet
 */
@WebServlet("/staff")
public class StaffServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private StaffService staff = new StaffService();
	 private DepartmentService dp=new DepartmentService();
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public StaffServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = request.getParameter("method");
		if (method == null) {
			method = "selectAll";
		}
		List<Department> deplist=dp.selectAll();
		switch (method) {
		case "selectAll":
			List<Staff> list = staff.selectAll();
			request.setAttribute("list", list);
			request.getRequestDispatcher("staffInfo.jsp").forward(request, response);
			break;
		case "delete":
			String staffSnoStr = request.getParameter("id");
			int staffSno = Integer.parseInt(staffSnoStr);
			staff.delete(staffSno);
			response.sendRedirect("/salary_manage/staff");
			break;
		case "findById":
			staffSnoStr = request.getParameter("id");
			staffSno = Integer.parseInt(staffSnoStr);
			request.setAttribute("staff", staff.findById(staffSno));
			request.setAttribute("list", deplist);
			request.getRequestDispatcher("updateStaff.jsp").forward(request, response);
			break;
		case "add":
			request.setAttribute("list", deplist);
			request.getRequestDispatcher("addStaff.jsp").forward(request, response);
			break;
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String method = request.getParameter("method");
		switch (method) {
		case "add":
			String staffSnoStr = request.getParameter("staffSno");
			int staffSno = Integer.parseInt(staffSnoStr);
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			String name = request.getParameter("name");
			String sex= request.getParameter("sex");
			String departmentSnoStr=request.getParameter("departmentSno");
			int departmentSno = Integer.parseInt(departmentSnoStr);
			String post = request.getParameter("post");
			staff.add(staffSno, name,sex ,departmentSno,post,username, password);
			break;
		case "update":
			staffSnoStr = request.getParameter("staffSno");
			staffSno = Integer.parseInt(staffSnoStr);
			name = request.getParameter("name");
			sex = request.getParameter("sex");
			departmentSnoStr = request.getParameter("departmentSno");
			departmentSno = Integer.parseInt(departmentSnoStr);
			post = request.getParameter("post");
			staff.update(staffSno, name, sex, departmentSno,post);
			break;
		}
		response.sendRedirect("/salary_manage/staff");
	}

}
